package com.liukai.bishe.ui.fragment;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionMessageList;
import com.liukai.bishe.model.doman.transaction.TransactionTotal;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionUserTimePresenter;
import com.liukai.bishe.presenter.transactionPresenter.impl.ITransactionPresenterImpl;
import com.liukai.bishe.presenter.transactionPresenter.impl.ITransactionUserTimePresenterImpl;
import com.liukai.bishe.ui.activity.loginAndRegister.LoginActivity;
import com.liukai.bishe.ui.adapter.TransationAdapter;
import com.liukai.bishe.ui.dialog.AddTranscationDialog;
import com.liukai.bishe.ui.service.BiSheService;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.ITransactionCallBack;
import com.liukai.bishe.view.ITransactionUserTimeCallBack;

import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class TransactionFragment extends BaseFragment implements ITransactionCallBack,ITransactionUserTimeCallBack {


    private Context mContext;

    @BindView(R.id.transactionRecyclerView)
    public RecyclerView mRecyclerView;

    @BindView(R.id.btnAdd)
    public FloatingActionButton btnAdd;

//    @BindView(R.id.input)
//    public EditText input;

    private ITransactionPresenter mITransactionPresenter; // 操作事务的Presenter
    private TransationAdapter mTransationAdapter;
    private List<Transaction> mTransactionList;
    private ITransactionUserTimePresenter mITransactionUserTimePresenter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_transaction;
    }

    /**
     * 获取上下文
     * @param view
     * @return
     */
    @Override
    protected Context getFragmentContext(View view) {
        mContext = view.getContext();
        return null;
    }

    @Override
    protected void initAdapter(View view) {
        mTransationAdapter = new TransationAdapter(mContext,mTransactionList,mITransactionPresenter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mTransationAdapter);
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initDate() {
        mTransactionList = Constants.getTransactionList();
        mITransactionPresenter = new ITransactionPresenterImpl();
        mITransactionPresenter.registerCallback(this);

        mITransactionUserTimePresenter = new ITransactionUserTimePresenterImpl();
        mITransactionUserTimePresenter.registerCallback(this);

    }

    @Override
    protected void initListener() {
        super.initListener();

        btnAdd.setOnClickListener(view -> {

//            String etInput = input.getText().toString();
//
//            Intent intent = new Intent(mContext,BiSheService.class);
////            intent.putExtra("input",etInput);
//            ContextCompat.startForegroundService(mContext,intent);

            AddTranscationDialog addTranscationDialog = new AddTranscationDialog(mContext,mITransactionPresenter);
//            addTranscationDialog.setHandler(handler);
            addTranscationDialog.show();
        });

    }


    @Override
    public void resultTrsaction(Object object) {

        TransactionMessageList transactionMessageList = (TransactionMessageList)object;
//        String code = transactionMessageList.getCode();
//        if(code.equals("4001")){
        List<Transaction> transactions = transactionMessageList.getTransactionList();
        ToastUtil.log(mContext,"TransactionFragment",transactions.toString());
        Constants.setTransactionList(transactions);
        mTransactionList = Constants.getTransactionList();
//            mTransationAdapter.setTransactionList(mTransactionList);
        mTransationAdapter.notifyDataSetChanged();
//        }
        mITransactionUserTimePresenter.findTransactionUserTimeByUserName(Constants.getUserInfo().getUsername());
        mITransactionPresenter.findTransactionTotal(Constants.getUserInfo().getUsername());
        ToastUtil.show(mContext,"添加成功");

    }

    /**
     * 获取服务器返回的该用户每一项任务总的执行情况
     * @param transactionTotalList
     */
    @Override
    public void resultTransactionTotal(List<TransactionTotal> transactionTotalList) {
        Constants.setTransactionTotalList(transactionTotalList);
    }

    @Override
    protected void release() {
        super.release();
        if (mITransactionPresenter != null) {
            mITransactionPresenter.unregisterCallback(this);
        }
        if (mITransactionUserTimePresenter != null) {
            mITransactionUserTimePresenter.unregisterCallback(this);
        }
    }

    @Override
    public void result(Object object) {

    }

    @Override
    public void findTransactionUserTimeByUserName(List<TransactionUseTime> transactionUseTimes) {
        Constants.setTransactionUseTimeList(transactionUseTimes);
    }
}
